﻿#include "OperatorsJunctionLine.h"

#include "PickDataProvider/GUIPickInfo.h"
#include "PickDataProvider/PickedDataProvider.h"
#include "PickDataProvider/PickedData.h"
#include "GUIDialog/GUIJunctionDialog/SEALineJunctionDialog.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscNodeList.h"

#include "OperatorsInterface/GraphEventOperator.h"

#include "updateBrowserTable.h"

#include <QMessageBox>

namespace ModelOper
{
    bool OperatorsJunctionLine::execGUI()
    {
        //获取离散数据
        Acoustics::FITKAcousticsDiscModel* discModel = FITKAPP->getGlobalData()->getMeshData<Acoustics::FITKAcousticsDiscModel>();
        if (discModel == nullptr)return false;

        if (_senderName == "actionCreateJunctionLine") {
            GraphData::PickedDataProvider* provider = GraphData::PickedDataProvider::getInstance();
            if (provider == nullptr)return false;
            //清除拾取对象所有的连接
            QObject::disconnect(provider, SIGNAL(sig_pickedOver()), nullptr, nullptr);

            GUI::GUIPickInfoStru pickStr;
            pickStr._pickObjType = GUI::GUIPickInfo::PickObjType::POBJDiscNode;
            pickStr._pickMethod = GUI::GUIPickInfo::PickMethod::PMIndividually;
            GUI::GUIPickInfo::ClearPickInfo();
            GUI::GUIPickInfo::SetPickInfo(pickStr);

            connect(provider, SIGNAL(sig_pickedOver()), this, SLOT(slotPickOverEvent()));
        }
        else if (_senderName == "actionEditJunctionLine") {
            int objID = -1;
            this->argValue(objID_String, objID);
            GUI::SEALineJunctionDialog* dialog = new GUI::SEALineJunctionDialog(objID, this, false);
            dialog->show();
        }
        else if (_senderName == "actionDeleteBatchLine") {
            if (discModel->getJunctionManager()) {
                QList<Acoustics::FITKAbstractJunction*> seaPointJunList =
                    discModel->getJunctionManager()->getJointsByType(Acoustics::FITKAbstractJunction::JunctionType::SEALineJunction);
                for (Acoustics::FITKAbstractJunction* seaPointJun : seaPointJunList) {
                    discModel->getJunctionManager()->removeDataObj(seaPointJun);
                }
            }
            EventOper::GraphEventOperator* operGraph = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
            if (operGraph) {
                operGraph->updateJunctions();
            }
        }
        return false;
    }

    bool OperatorsJunctionLine::execProfession()
    {
        int objId = -1;
        this->argValue(objID_String, objId);

        // Added by ChengHaotian 刷新可视化对象。
        //@{
        EventOper::GraphEventOperator* operGraph = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
        if (operGraph)
        {
            operGraph->updateGraph(objId);
        }
        //@}

        //更新表格
        ModelOper::updateBrowserTable();

        return false;
    }

    void OperatorsJunctionLine::slotPickOverEvent()
    {
        GraphData::PickedDataProvider* provider = GraphData::PickedDataProvider::getInstance();
        if (provider == nullptr)return;
        disconnect(provider, SIGNAL(sig_pickedOver()), this, SLOT(slotPickOverEvent()));

        QList<GraphData::PickedData*> pickList = provider->getPickedList();
        QList<int> idList = {};
        for (GraphData::PickedData* pickData : pickList) {
            if (pickData == nullptr)continue;
            for (auto str : pickData->getPickedDiscNodes()) {
                idList.append(str._id);
            }
        }

        //选取点少于两个
        if (idList.size() < 2) {
            QMessageBox box(QMessageBox::Warning, "", QString(tr("Too few nodes picked!")), QMessageBox::Ok);
            box.exec();
            GUI::GUIPickInfo::ClearPickInfo();
            return;
        }

        //创建有效连接点
        Acoustics::FITKAcousticsDiscModel* discModel = FITKAPP->getGlobalData()->getMeshData<Acoustics::FITKAcousticsDiscModel>();
        if (discModel == nullptr && discModel->getSEAModel() == nullptr)return;
        Acoustics::FITKAcousticsDiscNodeList* nodeList = discModel->getNodes();
        QList<int> junNodes = {};
        for (int nodeID : idList) {
            Core::FITKNode* point = nodeList->getNodeByID(nodeID);
            //SEA中追加连接点
            int createNodeId = nodeList->addNode(point->x(), point->y(), point->z());
            junNodes.append(createNodeId);
        }

        GUI::SEALineJunctionDialog* dialog = new GUI::SEALineJunctionDialog(-1, this, true);
        dialog->setSubsysPoints(idList);
        dialog->setNodes(junNodes);
        dialog->show();

        GUI::GUIPickInfo::ClearPickInfo();
    }
}